package com.qdu.program.controller;

import com.qdu.program.entity.Admin;
import com.qdu.program.entity.Read;
import com.qdu.program.entity.User;
import com.qdu.program.entity.Userinfo;
import com.qdu.program.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/Admin")
@Controller
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    ReadService readService;

    @Autowired
    CollectionsService collectionsService;

    @Autowired
    UserService userService;

    @Autowired
    UserinfoService userinfoService;

    @RequestMapping("toLogin")
    public String toLogin() {
        return "adminLogin";
    }

    @RequestMapping("adminLogin")
    public String adminLogin(String adminId, String adminPassword, Model model) {
        Admin admin = adminService.selectByPrimaryKey(adminId);
        if (admin == null) {
            model.addAttribute("msg","改用户不存在");
            return  "adminLogin";
        } else {
            if (admin.getAdminPassword().equals(adminPassword)){
            }else{
                model.addAttribute("msg","密码错误");
                return  "adminLogin";
            }
        }
        return "adminFunction";
    }

    @RequestMapping("/toManageUser")
    public String toManageUser(Model model){
        List<User> userList = userService.getAllUser();
        model.addAttribute("userList",userList);
        return "manageUser";
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public void deleteUser(String userNumber){
        collectionsService.deleteByUserNumber(userNumber);
        List<Read> readList = readService.selectReadByUserNumber(userNumber);
        for(Read read : readList){
            collectionsService.deleteByReadId(read.getReadId());
        }
        readService.deleteByUserNumber(userNumber);

        userinfoService.deleteByPrimaryKey(userNumber);
        userService.deleteByPrimaryKey(userNumber);
    }

    @RequestMapping("/searchUser")
    @ResponseBody
    public Userinfo getUserinfo(String userNumber){
        return userinfoService.selectByPrimaryKey(userNumber);
    }

    @RequestMapping("/modifyUser")
    @ResponseBody
    public void modifyUser(String userNumber,String userName,String userEmail,String userTel){
        Userinfo userInfo = userinfoService.selectByPrimaryKey(userNumber);
        userInfo.setUserName(userName);
        userInfo.setUserinfoEmail(userEmail);
        userInfo.setUserinfoTel(userTel);
        userinfoService.updateByPrimaryKey(userInfo);
        User user = userService.selectByPrimaryKey(userNumber);
        user.setUserName(userName);
        userService.updateByPrimaryKey(user);
    }
    @RequestMapping("/toManageRead")
    public String toManageRead(Model model){
        List<Read> readList = readService.getAllRead();
        model.addAttribute("readList",readList);
        return "manageRead";
    }

    @RequestMapping("/modifyRead")
    @ResponseBody
    public void modifyRead(Integer readId,String readText,String userNumber){
        Read read = readService.selectByPrimaryKey(readId);
        read.setReadId(readId);
        read.setReadText(readText);
        read.setUserNumber(userNumber);
        readService.updateByPrimaryKey(read);
    }

    @RequestMapping("/searchRead")
    @ResponseBody
    public Read getReadInfo(Integer readId){
        return readService.selectByPrimaryKey(readId);
    }

    @RequestMapping("/deleteRead")
    @ResponseBody
    public void deleteRead(Integer readId){
        collectionsService.deleteByReadId(readId);
        readService.deleteByPrimaryKey(readId);
    }
}

